package my.app.company;

import my.app.FavTabView;
import my.app.R;
import my.app.ServiceClient;
import my.app.Settings;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FavDialog extends Dialog{

	Company company = null;
	String phoneNumber = null;
	String url = null;
	
	public FavDialog(Context context, final Company company) {
		
		
		super(context);
		
		
		this.company = company;

		setContentView(R.layout.company_dialog);
		
		
		
		
		
		setTitle(" :: Company Information ::.");
		
		//Set Cab Icon
		ImageView iv = (ImageView) findViewById(R.id.taxi_dialog_introduction_image);
		iv.setImageResource(ServiceClient.getDrawableId(company.getIcon()));
		
		//Set com name..
		TextView plate = (TextView) findViewById(R.id.taxi_dialog_introduction_companyname);
		plate.setText(company.getName());
		
		//Set Telephone.
		TextView phone = (TextView) findViewById(R.id.taxi_dialog_phone_text);
		phone.setText(company.getTel());
		phoneNumber = company.getTel().substring(1);
		
		//Set Rating Stars..
		ImageView stars = (ImageView) findViewById(R.id.taxi_dialog_rating_1);
		stars.setImageResource(ServiceClient.getStarID(company.getRating().toString()));
		
		//Set Rating Count...
		TextView rat = (TextView) findViewById(R.id.taxi_dialog_rating_text);
		rat.setText("( "+company.getRatingCount().toString()+" ratings )");
		
		//Set Services..
		TextView servs = (TextView) findViewById(R.id.taxi_dialog_services);
		String s = "";
		for (String ss : company.getServices()) {
			
			s = s+ss+", ";
		}
		s = s.substring(0, s.length()-2);
		s = s.toUpperCase();
		servs.setText(s);
		
		//Set Licance Plate.
		TextView licence = (TextView) findViewById(R.id.taxi_dialog_inf_licanceplate);
		licence.setText("Email:  "+company.getEmail());
		
		//Set car model.
		TextView car = (TextView) findViewById(R.id.taxi_dialog_inf_carmodel);
		car.setText("Web:  "+company.getWeb());
		this.url = company.getWeb();
		
		//Set Distace..
		TextView distance = (TextView) findViewById(R.id.taxi_dialog_inf_distance);
		distance.setText("Distace:  "+company.getDistance().toString()+ " KM   (bird's eye)");
		
		//Set Comments
		TextView commnet = (TextView) findViewById(R.id.taxi_dialog_inf_company);
		commnet.setText("Address:  "+company.getAddress());
		
		
		
		
		
		
		
		//Listeners...
		//Listener Ratings.
		LinearLayout lrating = (LinearLayout) findViewById(R.id.taxi_dialog_rating);
		lrating.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				CompanyRating c = new CompanyRating(getContext());
				c.show();
				
			}
		});
		
		//Listener OK Button..
		Button b = (Button) findViewById(R.id.taxi_dialog_okbutton);
		b.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	dismiss();
            }
        });
		
		//Listener Phone..
		LinearLayout lphone = (LinearLayout) findViewById(R.id.taxi_dialog_phone);
		lphone.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String uri = "tel:"+phoneNumber;
				Intent intent = new Intent(Intent.ACTION_CALL);
				intent.setData(Uri.parse(uri));
				getContext().startActivity(intent);
				
			}
		});
		
		//Listener Web Site.
		car.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String uri = url;
				Intent intent = new Intent(Intent.ACTION_VIEW);
				intent.setData(Uri.parse(uri));
				getContext().startActivity(intent);
			}
		});
		//Listener Add Favorites Button
		Button bfav = (Button) findViewById(R.id.taxi_dialog_addfavbutton);
		bfav.setText("Remove");
		bfav.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				ServiceClient s = ServiceClient.getInstance(getContext());
				s.removeFavorite(Settings.getInstance().getUsername(), company.getId());
				
				
				
				//Set flag favorites
				s.setFlagFavorites(1);
				
				
				AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
				builder.setMessage("Company Removed from Favorites")
				       .setCancelable(false)
				       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
				           public void onClick(DialogInterface dialog, int id) {
				                
				           }
				       });
				AlertDialog alert = builder.create();
				alert.show();
				
			}
		});
		
		
		
		
		//Dialogs View Settings.
		Window w = getWindow();
		WindowManager.LayoutParams lp = w.getAttributes();
		lp.width = WindowManager.LayoutParams.FILL_PARENT;
		lp.height = WindowManager.LayoutParams.FILL_PARENT;
		w.setAttributes(lp);


		
		

	}
	
	
    
	
	
	
	


	
	

	

}
